﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Action.MetaData.Entity;
using T.FAS.Base.Dao;
using Teld.Core.DAC.Service;
using Teld.Core.DataAccessEx.Core;

namespace T.FAS.Action.Dev.Dao
{
    public class ActionACLDefineELBDao : DaoService<ACLDefine>
    {
        public override string TableName => "FAS_ACLDefine";
        public string ACLDetailTableName = "FAS_ACLDetail";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName
            => DaoUtil.GetExceptionColumn<ACLDefine>(
                new List<string>
                {
                }
            );
        public ActionACLDefineELBDao() : base()
        {
            DBName = "TeldFAS";
        }
        public override Dictionary<string, PropertyInfo> GetColumns()
        {
            return DaoServiceExtension.GetAllColumns<ACLDefine>(base.GetColumns());
        }
        public override ACLDefine ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("ACLMetaDataType"))
                columns.Remove("ACLMetaDataType");

            var data = base.ConvertToObject(columns, row);

            data.ACLMetaDataType = (ACLMetaDataType)Enum.Parse(typeof(ACLMetaDataType), Convert.ToString(row["ACLMetaDataType"]));

            return data;
        }

        private ActionACLDetailELBDao actionACLDetailELBDao => new ActionACLDetailELBDao();
        public override Dictionary<string, object> GetDataMapping(ACLDefine data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("ACLMetaDataType");
            mapping.Add("ACLMetaDataType", (int)data.ACLMetaDataType);

            return mapping;
        }

        /// <summary>
        /// 根据Action元数据ID删除ACL定义及明细表数据
        /// </summary>
        /// <param name="actionID"></param>
        public void DeleteActionACLDefineByActionID(string actionID)
        {
            var parameter = DB.MakeInParam("@ActionMetaDataID", TeldDbDataType.VarChar, actionID);

            DB.ExecSqlStatement(string.Format("delete from {0} where ACLDefineID in (select ID from {1} where ACLMetaDataType =0 and ACLMetaDataID = @ActionMetaDataID)", ACLDetailTableName,this.TableName),parameter
                );

            DB.ExecSqlStatement(string.Format("delete from {0} where ACLMetaDataType =0 and ACLMetaDataID = @ActionMetaDataID", this.TableName), parameter);
        }

        public List<ACLDefine> GetACLDefineByFilter(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }

        public ACLDefine GetACLDefineByID(string aclID)
        {
            throw new NotImplementedException();
        }

        public ACLDefine GetACLDefineByActionID(string actionID)
        {
            var result = base.GetObjectsByFilter($"ACLMetaDataType='0' and ACLMetaDataID='{actionID}'");
            if (result == null || result.Count<=0)
                return null;
            var aclDefine = result[0] as ACLDefine;
            aclDefine.ACLDetails = new List<ACLDetail>();
            var detailResult = actionACLDetailELBDao.GetACLDetailsByACLDefID(aclDefine.ID);
            if (detailResult != null)
            {
                detailResult.ForEach(x => aclDefine.ACLDetails.Add(x as ACLDetail));
            }
            return aclDefine;
        }

        public void AddACLDefine(ACLDefine acl)
        {
            throw new NotImplementedException();
        }

        public void UpdateACLDefine(ACLDefine acl)
        {
            throw new NotImplementedException();
        }

        public void DeleteACLDefine(string aclID)
        {
            throw new NotImplementedException();
        }

        public int CheckUnique(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }

        public List<ACLDetail> GetACLDetailByFilter(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }

        public ACLDetail GetACLDetailByID(string id)
        {
            throw new NotImplementedException();
        }

        public void AddACLDetail(ACLDetail detail)
        {
            throw new NotImplementedException();
        }

        public void AddACLDetailList(ArrayList details)
        {
            throw new NotImplementedException();
        }

        public void UpdateACLDetail(ACLDetail detail)
        {
            throw new NotImplementedException();
        }

        public void DeleletACLDetailBy(string aclID)
        {
            throw new NotImplementedException();
        }
    }
}
